package cn.lyvust.demo.shili11;

public class TickRunnable implements Runnable{
    private int count=100;  //记录剩余票数
    private int num = 0;   //记录买到第几张票
    private String lock = "lock";

    @Override
    public void run() {
        while(count > 0) {
            synchronized (this) {
                //没有余票时，跳出循环
                if (count <= 0) {
                    System.out.println("票卖完了");
                    return;
                }
                //第一步：修改数据
                count--;
                num++;
                try {
                    Thread.sleep(100); //模拟网络延时
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //第二步：显示信息
                System.out.println(Thread.currentThread().getName() + "抢到第" + num + "张票，剩余" + count + "张票！");
            }
        }
    }
}
